%macro get_f1120s_k1;

	
	proc printto log = "&logdir/get_f1120s_k1.txt" new;
	run;
	

		/****************************
		*	Get the tins 			*
		*	into 'tinlist'			*
		****************************/
		%make_tinlist;

		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table sample as
			select * from connection to iq 
			(
			select
				b.[redacted] as f1120s_div,
				b.[redacted] as f1120s_int,
				b.[redacted] as f1120s_royal,
				b.[redacted] as f1120s_st,
				b.[redacted] as f1120s_lt,
				coalesce(b.[redacted],0) + coalesce(b.[redacted],0) + coalesce(b.[redacted],0)
					- coalesce(b.[redacted],0) as f1120s_ord,
				a.person_id,
				b.[redacted] as payer_id,
				b.[redacted] as formdate
			from #tinlist as a
				inner join [redacted] as b
					on(a.person_id = b.[redacted])
			where b.[redacted] = &base_yr
				and b.[redacted] <= 4
			order by a.person_id,
				payer_id,
				formdate
			);
			disconnect from iq;
			quit;

		data sample;
			set sample;
			by person_id payer_id;
			if last.payer_id;
			drop formdate;
		run;

		proc means data = sample nway noprint;
			class person_id;
			output out = rlib.f1120s_k1(drop=_type_ _freq_)
				sum(f1120s_div f1120s_int f1120s_st f1120s_lt f1120s_ord f1120s_royal) =
					f1120s_div f1120s_int f1120s_st f1120s_lt f1120s_ord f1120s_royal;
		run;

		

	proc printto;
	run;
%mend;
